﻿using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Web.UI;
using System.Collections;
namespace OF.Controls
{
    /// <summary>
    /// 文件名称：排序控件接口
    /// </summary>
    public interface ISort : ISerializable
    {
        /// <summary>
        /// 获取sql排序语句
        /// </summary>
        /// <returns></returns>
        string GetSqlSort();
        /// <summary>
        /// 获取或设置sql排序是否升序
        /// </summary>
        bool IsAsc { get; set; }
        /// <summary>
        /// 获取或设置sql排序字段
        /// </summary>
        string SortField { get; set; }
        /// <summary>
        /// 获取或设置sql排序分组
        /// </summary>
        string SortGroupName { get; set; }
        /// <summary>
        /// 控件显示的文本
        /// </summary>
        string Text { get; set; }
    }

    /// <summary>
    /// 文件名称：排序控件集合
    /// </summary>
    public class SortColletion : IEnumerable<ISort>
    {
        private const string OrderDirection = "△▽ ";
        protected List<ISort> lstSorts = new List<ISort>();
        private int index;
        private readonly Control contain;

        /// <summary>
        /// 从Page对象或容器控件对象实例化一个排序控件集合
        /// </summary>
        /// <param name="contain"></param>
        public SortColletion(Control contain)
        {
            this.contain = contain;
        }
        /// <summary>
        /// 返回搜索控件集合的组合搜索条件的sql where语句表示
        /// </summary>
        /// <returns></returns>
        public string GetSqlOrder(ISort sort, string sortGroupName = "")
        {
            return GetSortEntity(sort, sortGroupName).GetSqlSort();
        }
        /// <summary>
        /// 返回搜索控件集合的组合搜索条件的排序实体
        /// </summary>
        /// <returns></returns>
        public ISort GetSortEntity(ISort sort, string sortGroupName = "")
        {
            LoadSorts(sortGroupName);
            SetOrder(sort);
            if (index >= 0 && index < lstSorts.Count)
                return lstSorts[index];
            return null;
        }
        /// <summary>
        /// 返回一个循环访问集合的枚举器。
        /// </summary>
        /// <returns>可用于循环访问集合的 System.Collections.Generic.IEnumerator。</returns>
        public IEnumerator<ISort> GetEnumerator()
        {
            return ((IEnumerable<ISort>)lstSorts).GetEnumerator();
        }

        private void LoadSorts(string searcherGroupName = "")
        {
            lstSorts.Clear();

            if (contain.HasControls())
            {
                GetControls(contain, searcherGroupName);
            }
        }
        /// <summary>
        /// 递归加载排序控件
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="groupName"></param>
        private void GetControls(Control parent, string groupName = "")
        {
            ControlCollection controls = parent.Controls;
            foreach (Control ctl in controls)
            {
                if (ctl is ISort)
                {
                    var sort = ctl as ISort;
                    if (string.IsNullOrWhiteSpace(groupName))
                    {
                        lstSorts.Add(sort);
                    }
                    else if (sort.SortGroupName == groupName)
                    {
                        lstSorts.Add(sort);
                    }
                }
                else if (ctl.HasControls())
                {
                    GetControls(ctl, groupName);
                }
            }
        }

        private void SetOrder(ISort sort)
        {
            index = lstSorts.IndexOf(sort);
            foreach (ISort s in lstSorts)
            {
                if (s.SortField == sort.SortField)
                {
                    s.Text = s.Text.Trim(OrderDirection.ToCharArray()) + " " + OrderDirection[sort.IsAsc ? 0 : 1];
                }
                else
                {
                    s.Text = s.Text.Trim(OrderDirection.ToCharArray());
                }
            }
        }

        /// <summary>
        /// 返回一个循环访问集合的枚举器。
        /// </summary>
        /// <returns>可用于循环访问集合的 System.Collections.Generic.IEnumerator。</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
